<?php

namespace App\Http\Controllers\Admin;

use App\Jobs\BookStockRemindUserJob;
use App\Models\Book;
use Illuminate\Http\Request;
use DB;
use Cache;

class BookController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('check_admin_login');
    }

    public function remindBooks(Request $request)
    {
        $page                         = intval($request->get('page',1));
        $page                         = max($page ,1);
        $page_size                    = intval($request->get('limit',10));

        $builder = Book::query()->with('parent_catgeory')->where('deleted',0)->where('remind_users_count','>',0);

        $columns = ['id','title','category_pid','author','image','total_stock','new_book_stock','good_book_stock','medium_book_stock','remind_users_count','isbn'];
        $list = $builder->orderBy('remind_users_count','desc')->orderBy('id','desc')->paginate($page_size,$columns,'page',$page);

        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $items[] = [
                    'id'                             => $item->id,
                    'title'                          => $item->title,
                    'category_pid'                   => $item->category_pid,
                    'category_pid_title'             => optional($item->parent_catgeory)->title ? optional($item->parent_catgeory)->title : '',
                    'author'                         => $item->author,
                    'image'                          => $item->image,
                    'total_stock'                    => $item->total_stock,
                    'new_book_stock'                 => $item->new_book_stock,
                    'good_book_stock'                => $item->good_book_stock,
                    'medium_book_stock'              => $item->medium_book_stock,
                    'remind_users_count'             => $item->remind_users_count,
                    'isbn'                           => $item->isbn,
                    'medium_book_stock2'             => $item->medium_book_stock2,
                    'medium_book_stock3'             => $item->medium_book_stock3,
                    'medium_book_stock4'             => $item->medium_book_stock4,
                    'medium_book_stock5'             => $item->medium_book_stock5,
                    'medium_book_stock6'             => $item->medium_book_stock6,
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }

    public function index(Request $request)
    {
        $page                         = intval($request->get('page',1));
        $page                         = max($page ,1);
        $page_size                    = intval($request->get('limit',10));
        $type                         = intval($request->get('type',1));  //0全部 1已上架  2无库存 3已下架 4聚合回收 5聚合不回收
        $category_pid                 = intval($request->get('category_pid'));
        $homepage_recommend_module_id = intval($request->get('homepage_recommend_module_id'));
        $category_id                  = intval($request->get('first-category-ids'));
        $is_recycle                   = intval($request->get('is_recycle'));  //0全部 1支持 2不支持
        $keyword                      = trim($request->get('keyword'));
        $total_stock_sort             = intval($request->get('total_stock_sort'));  //1库存从高到低   2库存从低到高
        $sales_total_sort             = intval($request->get('sales_total_sort'));  //1销量从高到低   2销量从低到高


        //DB::enableQueryLog();
        //DB::getQueryLog();
        $builder = Book::query()->with('parent_catgeory','homepage_recommend_modules')->where('deleted',0);

        if($type == 0)
        {
            //$builder->where('state',1);
        }

        if($type == 1)
        {
            $builder->where('state',1);
        }
        elseif($type == 2)
        {
            $builder->where('state',1)->where('total_stock',0);
        }
        elseif($type == 3)
        {
            $builder->where('state',2);
        }
        elseif($type == 4)
        {
            $builder->where('from',2)->where('state',2)->where('is_recycle',1);
        }
        elseif($type == 5)
        {
            $builder->where('from',2)->where('state',2)->where('is_recycle',2);
        }

        if(!empty($category_pid))
        {
            $builder->where('category_pid',$category_pid);
        }

        if(!empty($is_recycle))
        {
            $builder->where('is_recycle',$is_recycle);
        }

        if(!empty($homepage_recommend_module_id))
        {
            $builder->where(function ($query) use ($homepage_recommend_module_id) {
                $query->whereRaw("FIND_IN_SET($homepage_recommend_module_id,homepage_recommend_module_ids)");
            });
        }

        if(!empty($category_id))
        {
            $builder->where(function ($query) use ($category_id) {
                $query->whereRaw("FIND_IN_SET($category_id,category_ids)");
            });
        }

        if(!empty($keyword))
        {
            $like = '%'.$keyword.'%';
            $builder->where(function ($query) use ($like) {
                $query->where('title', 'like', $like)
                    ->orWhere('author', 'like', $like)
                    ->orWhere('isbn','like',$like);
            });
        }

        //$columns = ['id','title','category_pid','author','image','total_stock','old_price','new_book_price','new_book_stock','good_book_price','good_book_stock','medium_book_price','medium_book_stock','is_recycle','recyle_price','state','homepage_recommend_module_ids','sales_total'];
        $columns = ['*'];

        if($total_stock_sort == 1)
        {
            $builder->orderBy('total_stock','desc');
        }
        elseif($total_stock_sort == 2)
        {
            $builder->orderBy('total_stock','asc');
        }

        if($sales_total_sort == 1)
        {
            $builder->orderBy('sales_total','desc');
        }
        elseif($sales_total_sort == 2)
        {
            $builder->orderBy('sales_total','asc');
        }

        $list = $builder->orderBy('id','desc')->paginate($page_size,$columns,'page',$page);
        //dd($list);

        //echo $builder->toSql(); // output sql query
        //die;
        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $items[] = [
                    'id'                             => $item->id,
                    'title'                          => $item->title,
                    'category_pid'                   => $item->category_pid,
                    'category_ids'                   => $item->category_ids,
                    'category_pid_title'             => optional($item->parent_catgeory)->title ? optional($item->parent_catgeory)->title : '',
                    'author'                         => $item->author,
                    'sub_title'                      => $item->sub_title,
                    'score'                          => $item->score,
                    'image'                          => $item->image,
                    'author_intro'                   => $item->author_intro,
                    'old_price'                      => $item->old_price,
                    'total_stock'                    => $item->total_stock,
                    'publish_house'                  => $item->publish_house,
                    'publish_time'                   => $item->publish_time,
                    'publish_num'                    => $item->publish_num,
                    'publish_translator'             => $item->publish_translator,
                    'publish_package'                => $item->publish_package,
                    'publish_paper'                  => $item->publish_paper,
                    'publish_pages'                  => $item->publish_pages,
                    'publish_open'                   => $item->publish_open,
                    'isbn'                           => $item->isbn,
                    'desc'                           => $item->desc,
                    'catalog'                        => $item->catalog,
                    'new_book_price'                 => $item->new_book_price,
                    'new_book_stock'                 => $item->new_book_stock,
                    'good_book_price'                => $item->good_book_price,
                    'good_book_stock'                => $item->good_book_stock,
                    'medium_book_price'              => $item->medium_book_price,
                    'medium_book_stock'              => $item->medium_book_stock,
                    'is_recycle'                     => $item->is_recycle,
                    'recyle_price'                   => $item->recyle_price,
                    'sales_total'                    => $item->sales_total,
                    'state'                          => $item->state,
                    'homepage_recommend_module_ids'  => $item->homepage_recommend_module_ids,
                    'homepage_recommend_modules'     => $item->homepage_recommend_modules,
                    'is_sult'                        => $item->is_sult,
                    'score_num'                      => $item->score_num,
                    'medium_book_price2'              => $item->medium_book_price2,
                    'medium_book_stock2'              => $item->medium_book_stock2,
                    'medium_book_price3'              => $item->medium_book_price3,
                    'medium_book_stock3'              => $item->medium_book_stock3,
                    'medium_book_price4'              => $item->medium_book_price4,
                    'medium_book_stock4'              => $item->medium_book_stock4,
                    'medium_book_price5'              => $item->medium_book_price5,
                    'medium_book_stock5'              => $item->medium_book_stock5,
                    'medium_book_price6'              => $item->medium_book_price6,
                    'medium_book_stock6'              => $item->medium_book_stock6,
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }

    public function search(Request $request)
    {
        $keyword = trim($request->get('keyword'));


        $items = [];
        if(!empty($keyword))
        {
            $builder = Book::query()->select(['id','title','author','image','publish_time'])->where('deleted',0);

            $like = '%'.$keyword.'%';
            $builder->where(function ($query) use ($like) {
                $query->where('title', 'like', $like)
                    ->orWhere('author', 'like', $like)
                    ->orWhere('isbn','like',$like);
            });

            $list = $builder->orderBy('id','desc')->get();
            if(!empty($list))
            {
                foreach ($list as $item)
                {
                    $items[] = [
                        'id'     => $item->id,
                        'title'  => $item->title,
                        'author' => $item->author,
                        'image'  => $item->image,
                        'publish_time'  => $item->publish_time,
                    ];
                }
            }
        }

        $result = [
            'items' => $items
        ];
        return $this->ok($result);
    }

    public function store(Request $request)
    {
        $category_pid                  = intval($request->get('category_pid'));
        $category_ids                  = trim(trim($request->get('category_ids')),',');
        $title                         = trim($request->get('title'));
        $author                        = trim($request->get('author'));
        $sub_title                     = trim($request->get('sub_title'));
        $score                         = $request->get('score',0);
        $image                         = trim($request->get('image'));
        $author_intro                  = trim($request->get('author_intro'));
        $publish_house                 = trim($request->get('publish_house'));
        $publish_time                  = trim($request->get('publish_time'));
        $publish_num                   = trim($request->get('publish_num'));
        $publish_translator            = trim($request->get('publish_translator'));
        $publish_package               = trim($request->get('publish_package'));
        $publish_paper                 = trim($request->get('publish_paper'));
        $publish_pages                 = trim($request->get('publish_pages'));
        $publish_open                  = trim($request->get('publish_open'));
        $isbn                          = trim($request->get('isbn'));
        $desc                          = trim($request->get('desc'));
        $catalog                       = trim($request->get('catalog'));
        $old_price                     = $request->get('old_price');
        $new_book_price                = $request->get('new_book_price');
        $new_book_stock                = intval($request->get('new_book_stock'));
        $good_book_price               = $request->get('good_book_price');
        $good_book_stock               = intval($request->get('good_book_stock'));
        $medium_book_price             = $request->get('medium_book_price');
        $medium_book_stock             = intval($request->get('medium_book_stock'));
        $is_recycle                    = intval($request->get('is_recycle'));
        $is_sult                       = intval($request->get('is_sult'));
        $score_num                     = intval($request->get('score_num'));
        $recyle_price                  = $request->get('recyle_price');
        $homepage_recommend_module_ids = trim(trim($request->get('homepage_recommend_module_ids')),',');
        $medium_book_price2             = $request->get('medium_book_price2');
        $medium_book_stock2             = intval($request->get('medium_book_stock2'));
        $medium_book_price3             = $request->get('medium_book_price3');
        $medium_book_stock3             = intval($request->get('medium_book_stock3'));
        $medium_book_price4             = $request->get('medium_book_price4');
        $medium_book_stock4             = intval($request->get('medium_book_stock4'));
        $medium_book_price5             = $request->get('medium_book_price5');
        $medium_book_stock5             = intval($request->get('medium_book_stock5'));
        $medium_book_price6             = $request->get('medium_book_price6');
        $medium_book_stock6             = intval($request->get('medium_book_stock6'));

        if(empty($category_pid) || empty($category_ids) || empty($title) || empty($author) || empty($image) || empty($old_price))
        {
            return $this->err(1,200,'','参数错误');
        }

        $old_price = round($old_price,1);
        $new_book_price = round($new_book_price,1);
        $good_book_price = round($good_book_price,1);
        $medium_book_price = round($medium_book_price,1);
        $medium_book_price2 = round($medium_book_price2,1);
        $medium_book_price3 = round($medium_book_price3,1);
        $medium_book_price4 = round($medium_book_price4,1);
        $medium_book_price5 = round($medium_book_price5,1);
        $medium_book_price6 = round($medium_book_price6,1);

        //计算折扣
        $new_book_discount = round(($new_book_price/$old_price)*10,1);
        $good_book_discount = round(($good_book_price/$old_price)*10,1);
        $medium_book_discount = round(($medium_book_price/$old_price)*10,1);
        $medium_book_discount2 = round(($medium_book_price2/$old_price)*10,1);
        $medium_book_discount3 = round(($medium_book_price3/$old_price)*10,1);
        $medium_book_discount4 = round(($medium_book_price4/$old_price)*10,1);
        $medium_book_discount5 = round(($medium_book_price5/$old_price)*10,1);
        $medium_book_discount6 = round(($medium_book_price6/$old_price)*10,1);

        //总库存
        $total_stock = intval($good_book_stock + $new_book_stock + $medium_book_stock + $medium_book_stock2 + $medium_book_stock3 + $medium_book_stock4 + $medium_book_stock5 + $medium_book_stock6);
        $params = [
            'category_pid'                  => $category_pid,
            'category_ids'                  => $category_ids,
            'title'                         => $title,
            'author'                        => $author ? $author : '',
            'sub_title'                     => $sub_title ? $sub_title : '',
            'score'                         => $score ? $score : 0,
            'image'                         => $image,
            'author_intro'                  => $author_intro ? $author_intro : '',
            'publish_house'                 => $publish_house ? $publish_house : '',
            'publish_time'                  => $publish_time ? $publish_time : '',
            'publish_num'                   => $publish_num ? $publish_num : '',
            'publish_translator'            => $publish_translator ? $publish_translator : '',
            'publish_package'               => $publish_package ? $publish_package : '',
            'publish_paper'                 => $publish_paper ? $publish_paper : '',
            'publish_pages'                 => $publish_pages ? $publish_pages : '',
            'publish_open'                  => $publish_open ? $publish_open : '',
            'isbn'                          => $isbn,
            'desc'                          => $desc,
            'catalog'                       => $catalog,
            'old_price'                     => $old_price,
            'new_book_price'                => $new_book_price,
            'new_book_stock'                => $new_book_stock,
            'good_book_price'               => $good_book_price,
            'good_book_stock'               => $good_book_stock,
            'medium_book_price'             => $medium_book_price,
            'medium_book_stock'             => $medium_book_stock,
            'is_recycle'                    => $is_recycle,
            'is_sult'                       => $is_sult,
            'recyle_price'                  => $recyle_price ? $recyle_price : 0.00,
            'homepage_recommend_module_ids' => $homepage_recommend_module_ids,
            'state'                         => 1,
            'created_at'                    => $this->time,
            'updated_at'                    => $this->time,
            'total_stock'                   => $total_stock,
            'new_book_discount'             => $new_book_discount,
            'good_book_discount'            => $good_book_discount,
            'medium_book_discount'          => $medium_book_discount,
            'score_num'                     => $score_num,
            'medium_book_price2'            => $medium_book_price2,
            'medium_book_stock2'            => $medium_book_stock2,
            'medium_book_discount2'         => $medium_book_discount2,
            'medium_book_price3'            => $medium_book_price3,
            'medium_book_stock3'            => $medium_book_stock3,
            'medium_book_discount3'         => $medium_book_discount3,
            'medium_book_price4'            => $medium_book_price4,
            'medium_book_stock4'            => $medium_book_stock4,
            'medium_book_discount4'         => $medium_book_discount4,
            'medium_book_price5'            => $medium_book_price5,
            'medium_book_stock5'            => $medium_book_stock5,
            'medium_book_discount5'         => $medium_book_discount5,
            'medium_book_price6'            => $medium_book_price6,
            'medium_book_stock6'            => $medium_book_stock6,
            'medium_book_discount6'         => $medium_book_discount6,
        ];

        DB::beginTransaction();    //主事务
        try {
            $book = Book::create($params);

            $category_ids_arr = $category_ids ? explode(',',$category_ids) : '';
            if(!empty($category_ids_arr))
            {
                $book->categories()->attach($category_ids_arr);
            }

            $module_ids_arr = $homepage_recommend_module_ids ? explode(',',$homepage_recommend_module_ids) : '';
            if(!empty($module_ids_arr))
            {
                $book->homepage_recommend_modules()->attach($module_ids_arr);
            }

            DB::commit();
            return $this->ok(['book_id'=> $book->id],'添加成功');
        } catch (\Exception $e) {
            DB::rollBack();
            //echo $e->getMessage();
            return $this->err(1,200,$e->getMessage(),'添加失败');
        }
    }

    public function update(Request $request)
    {
        $id                            = intval($request->get('id'));
        $category_pid                  = intval($request->get('category_pid'));
        $category_ids                  = trim(trim($request->get('category_ids')),',');
        $title                         = trim($request->get('title'));
        $author                        = trim($request->get('author'));
        $sub_title                     = trim($request->get('sub_title'));
        $score                         = $request->get('score',0);
        $image                         = trim($request->get('image'));
        $author_intro                  = trim($request->get('author_intro'));
        $publish_house                 = trim($request->get('publish_house'));
        $publish_time                  = trim($request->get('publish_time'));
        $publish_num                   = trim($request->get('publish_num'));
        $publish_translator            = trim($request->get('publish_translator'));
        $publish_package               = trim($request->get('publish_package'));
        $publish_paper                 = trim($request->get('publish_paper'));
        $publish_pages                 = trim($request->get('publish_pages'));
        $publish_open                  = trim($request->get('publish_open'));
        $isbn                          = trim($request->get('isbn'));
        $desc                          = trim($request->get('desc'));
        $catalog                       = trim($request->get('catalog'));
        $old_price                     = $request->get('old_price');
        $new_book_price                = $request->get('new_book_price');
        $new_book_stock                = intval($request->get('new_book_stock'));
        $good_book_price               = $request->get('good_book_price');
        $good_book_stock               = intval($request->get('good_book_stock'));
        $medium_book_price             = $request->get('medium_book_price');
        $medium_book_stock             = intval($request->get('medium_book_stock'));
        $is_recycle                    = intval($request->get('is_recycle'));
        $is_sult                       = intval($request->get('is_sult'));
        $score_num                     = intval($request->get('score_num'));
        $recyle_price                  = $request->get('recyle_price');
        $homepage_recommend_module_ids = trim(trim($request->get('homepage_recommend_module_ids')),',');
        $medium_book_price2             = $request->get('medium_book_price2');
        $medium_book_stock2             = intval($request->get('medium_book_stock2'));
        $medium_book_price3             = $request->get('medium_book_price3');
        $medium_book_stock3             = intval($request->get('medium_book_stock3'));
        $medium_book_price4             = $request->get('medium_book_price4');
        $medium_book_stock4             = intval($request->get('medium_book_stock4'));
        $medium_book_price5             = $request->get('medium_book_price5');
        $medium_book_stock5             = intval($request->get('medium_book_stock5'));
        $medium_book_price6             = $request->get('medium_book_price6');
        $medium_book_stock6             = intval($request->get('medium_book_stock6'));

        if(empty($id) || empty($category_pid) || empty($category_ids) || empty($title) || empty($author) || empty($image) || empty($old_price))
        {
            return $this->err(1,200,'','参数错误');
        }




        $book = $this->_check_info($id);
        if(empty($book))
        {
            return $this->err(1,200,'','图书不存在');
        }

        $old_total_stock = $book->total_stock;  //当前库存

        $total_stock = intval($good_book_stock + $new_book_stock + $medium_book_stock + $medium_book_stock2 + $medium_book_stock3 + $medium_book_stock4 + $medium_book_stock5 + $medium_book_stock6);  //修改后的新库存

        $old_price         = round($old_price,1);
        $new_book_price    = round($new_book_price,1);
        $good_book_price   = round($good_book_price,1);
        $medium_book_price = round($medium_book_price,1);
        $medium_book_price2 = round($medium_book_price2,1);
        $medium_book_price3 = round($medium_book_price3,1);
        $medium_book_price4 = round($medium_book_price4,1);
        $medium_book_price5 = round($medium_book_price5,1);
        $medium_book_price6 = round($medium_book_price6,1);

        //计算折扣
        $new_book_discount    = round(($new_book_price/$old_price)*10,1);
        $good_book_discount   = round(($good_book_price/$old_price)*10,1);
        $medium_book_discount = round(($medium_book_price/$old_price)*10,1);
        $medium_book_discount2 = round(($medium_book_price2/$old_price)*10,1);
        $medium_book_discount3 = round(($medium_book_price3/$old_price)*10,1);
        $medium_book_discount4 = round(($medium_book_price4/$old_price)*10,1);
        $medium_book_discount5 = round(($medium_book_price5/$old_price)*10,1);
        $medium_book_discount6 = round(($medium_book_price6/$old_price)*10,1);

        $params = [
            'category_pid'                  => $category_pid,
            'category_ids'                  => $category_ids,
            'title'                         => $title,
            'author'                        => $author ? $author : '',
            'sub_title'                     => $sub_title ? $sub_title : '',
            'score'                         => $score ? $score : 0,
            'image'                         => $image,
            'author_intro'                  => $author_intro ? $author_intro : '',
            'publish_house'                 => $publish_house ? $publish_house : '',
            'publish_time'                  => $publish_time ? $publish_time : '',
            'publish_num'                   => $publish_num ? $publish_num : '',
            'publish_translator'            => $publish_translator ? $publish_translator : '',
            'publish_package'               => $publish_package ? $publish_package : '',
            'publish_paper'                 => $publish_paper ? $publish_paper : '',
            'publish_pages'                 => $publish_pages ? $publish_pages : '',
            'publish_open'                  => $publish_open ? $publish_open : '',
            'isbn'                          => $isbn,
            'desc'                          => $desc,
            'catalog'                       => $catalog,
            'old_price'                     => $old_price,
            'new_book_price'                => $new_book_price,
            'new_book_stock'                => $new_book_stock,
            'good_book_price'               => $good_book_price,
            'good_book_stock'               => $good_book_stock,
            'medium_book_price'             => $medium_book_price,
            'medium_book_stock'             => $medium_book_stock,
            'is_recycle'                    => $is_recycle,
            'is_sult'                       => $is_sult,
            'recyle_price'                  => $recyle_price ? $recyle_price : 0.00,
            'homepage_recommend_module_ids' => $homepage_recommend_module_ids,
            'updated_at'                    => $this->time,
            'total_stock'                   => $total_stock,
            'new_book_discount'             => $new_book_discount,
            'good_book_discount'            => $good_book_discount,
            'medium_book_discount'          => $medium_book_discount,
            'score_num'                     => $score_num,
            'medium_book_price2'            => $medium_book_price2,
            'medium_book_stock2'            => $medium_book_stock2,
            'medium_book_discount2'         => $medium_book_discount2,
            'medium_book_price3'            => $medium_book_price3,
            'medium_book_stock3'            => $medium_book_stock3,
            'medium_book_discount3'         => $medium_book_discount3,
            'medium_book_price4'            => $medium_book_price4,
            'medium_book_stock4'            => $medium_book_stock4,
            'medium_book_discount4'         => $medium_book_discount4,
            'medium_book_price5'            => $medium_book_price5,
            'medium_book_stock5'            => $medium_book_stock5,
            'medium_book_discount5'         => $medium_book_discount5,
            'medium_book_price6'            => $medium_book_price6,
            'medium_book_stock6'            => $medium_book_stock6,
            'medium_book_discount6'         => $medium_book_discount6,
        ];

        DB::beginTransaction();    //主事务
        try {
            //清除缓存
            //Cache::forget('book-shop:books:info:book-id:'.$id);

            $book->update($params);

            $category_ids_arr = $category_ids ? explode(',',$category_ids) : [];
            if(!empty($category_ids_arr))
            {
                $book->categories()->sync($category_ids_arr);
            }
            else
            {
                $book->categories()->detach();
            }

            $module_ids_arr = $homepage_recommend_module_ids ? explode(',',$homepage_recommend_module_ids) : [];

            if(!empty($module_ids_arr))
            {
                $book->homepage_recommend_modules()->sync($module_ids_arr);
            }
            else
            {
                $book->homepage_recommend_modules()->detach();
            }

            DB::commit();

            if($old_total_stock <= 0 && $total_stock > 0)
            {
                //任务分发 给到货提醒者发消息
                dispatch(new BookStockRemindUserJob($book));
            }


            return $this->ok(['book_id'=> $book->id],'修改成功');
        } catch (\Exception $e) {
            DB::rollBack();
            //echo $e->getMessage();
            return $this->err(1,200,$e->getMessage(),'修改失败');
        }
    }

    public function show(Request $request)
    {
        $id = intval($request->get('id'));
        $book = $this->_check_info($id);
        if(empty($book))
        {
            return $this->err(1,200,'','图书不存在');
        }
        return $this->ok($book->toArray());
    }


    private function _check_info($id)
    {
        $book = Book::query()->where('id',$id)->where('deleted',0)->first();
        return $book;
    }

    public function updateState(Request $request)
    {
        $state = intval($request->get('state')); //1=上架 2=下架
        $id   = intval($request->get('id'));

        if(empty($id) || empty($state) || !in_array($state,[1,2]))
        {
            return $this->err(1,200,'','参数错误');
        }

        $book = $this->_check_info($id);
        if(empty($book))
        {
            return $this->err(1,200,'','图书不存在');
        }

        //清除缓存
        //Cache::forget('book-shop:books:info:book-id:'.$id);

        $book->update([
            'state'      => $state,
            'updated_at' => $this->time
        ]);
        return $this->ok([],'操作成功');
    }

    public function destroy(Request $request)
    {
        $id = intval($request->get('id'));
        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $book = $this->_check_info($id);
        if(empty($book))
        {
            return $this->err(1,200,'','图书不存在');
        }

        //清除缓存
        //Cache::forget('book-shop:books:info:book-id:'.$id);

        $book->update([
            'deleted'      => 1,
            'deleted_at'   => $this->time
        ]);
        return $this->ok([],'删除成功');
    }

    public function batchAction(Request $request)
    {
        $ids  = trim(trim($request->get('ids')),',');
        $type = intval($request->get('type',1));  //1删除 2设置回收 3取消设置回收 4上架  5下架

        if(empty($ids) || empty($type) || !in_array($type,[1,2,3,4,5]))
        {
            return $this->err(1,200,'','参数错误');
        }


        $ids_arr = explode(',', $ids);
        $where_ids_arr = [];
        foreach ($ids_arr as $v)
        {
            if(is_numeric($v))
            {
                //清除缓存
                //Cache::forget('book-shop:books:info:book-id:'.$v);
                $where_ids_arr[] = $v;
            }
        }

        switch ($type)
        {
            case 1:
                $data = [
                    'deleted'    => 1,
                    'deleted_at' => $this->time
                ];
                break;
            case 2:
                $data = [
                    'is_recycle' => 1,
                    'updated_at' => $this->time
                ];
                break;
            case 3:
                $data = [
                    'is_recycle' => 2,
                    'updated_at' => $this->time
                ];
                break;
            case 4:
                $data = [
                    'state'      => 1,
                    'updated_at' => $this->time
                ];
                break;
            case 5:
                $data = [
                    'state'      => 2,
                    'updated_at' => $this->time
                ];
                break;
        }

        Book::query()->whereIn('id',$where_ids_arr)->update($data);
        return $this->ok([],'操作成功');
    }

    public function checkIsbn(Request $request)
    {
        $isbn = trim($request->get('isbn'));

        if(empty($isbn))
        {
            return $this->err(1,200,'','参数错误');
        }

        $book = Book::query()->where('deleted',0)->where('isbn',$isbn)->first();
        if(!empty($book))
        {
            return $this->err(1,200,'','ISBN已存在');
        }

        return $this->ok();
    }

    public function feedbackRecyleList(Request $request)
    {
        $page       = intval($request->get('page',1));
        $page       = max($page ,1);
        $page_size  = intval($request->get('limit',10));
        $type       = intval($request->get('type')); //0全部 1不回收 2已支持回收

        $builder = Book::query()->where('deleted',0)->where('feedback_recyle_count','>',0);

        if(!empty($type))
        {
            if($type == 2)
            {
                $builder->where('is_recycle',1);
            }
            else
            {
                $builder->where('is_recycle','<>',1);
            }
        }

        $columns = ['id','feedback_recyle_count','title','image','author','publish_time','isbn','is_recycle'];
        $list = $builder->orderBy('feedback_recyle_time','desc')->paginate($page_size,$columns,'page',$page);
        //dd($list);

        //echo $builder->toSql(); // output sql query
        //die;
        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $items[] = [
                    'id'                             => $item->id,
                    'title'                          => $item->title,
                    'author'                         => $item->author,
                    'image'                          => $item->image,
                    'publish_time'                   => $item->publish_time,
                    'isbn'                           => $item->isbn,
                    'is_recycle'                     => $item->is_recycle,
                    'feedback_recyle_count'          => $item->feedback_recyle_count,
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }
}
